<?php

namespace App\Admin\Extensions\Table;

use App\Admin\Repositories\ServiceOrder;
use App\Models\ServiceOrder as Model;
use App\Models\Worker;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Contracts\Support\Renderable;

class ServiceOrderTable extends LazyRenderable
{
    use LazyWidget;

    public function grid(): Grid
    {
        // 获取外部传递的参数
        $shop_id = $this->shop_id;
        return Grid::make(new ServiceOrder(['user','worker','shop']), function (Grid $grid) use ($shop_id) {
            $grid->disableCreateButton();
            $grid->disableBatchActions();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->model()->where("shop_id",$shop_id);
            $grid->disableCreateButton();
            $grid->scrollbarX();
            $grid->fixColumns(2, -2);
            $grid->column('order_id','订单号');
            $grid->column('worker.name','洗车技师')->limit(8);
            $grid->column('service.title','服务名称')->limit(8);
            $grid->column('user.nickname','用户昵称')->limit(8);
            $grid->column('address','用户地址')->limit(15);
            $grid->column('car.plate_number','车牌号')->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['color']})";
            });
            $grid->column('car.brand_name',"车辆品牌类型")->display(function ($value) {
                $car = $this->car;
                return "{$value}({$car['type']})";
            });
            $grid->column('car.name','客户姓名电话')->display(function ($value){
                $car = $this->car;
                return "{$value}({$car['phone']})";
            })->limit(10);
            $grid->column('amount');
            $grid->column('appoint_time','预约时间');
            $grid->column('coupon_price','优惠金额');
            $grid->column('order_time','下单时间');
            $grid->column('arrive_time','到达时间');
            $grid->column('finish_time','完成时间');
            $grid->column('status')->using(Model::getStatusList())->label(['default','primary','info','success','danger']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->like('order_id');
                $filter->like('user.nickname','用户昵称');
                $filter->equal('worker_id','洗车技师')->select(Worker::query()->pluck("name","id"));
                $filter->like('car->plate_number','车牌号');
                $filter->like('car->name','客户姓名');
                $filter->like('car->phone','客户电话');
                $filter->like('address','地址');
                $filter->between('order_time','下单时间')->date();
            });
            $grid->paginate(8);
        });
    }


}
